package com.edu.neusoft.phone_sale.controller;


import com.edu.neusoft.phone_sale.common.ResponseResult;
import com.edu.neusoft.phone_sale.common.enums.AppHttpCodeEnum;
import com.edu.neusoft.phone_sale.dto.UserDto;
import com.edu.neusoft.phone_sale.entity.User;
import com.edu.neusoft.phone_sale.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
@Api(value = "app端用户登录",tags = "app端用户登录")
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录接口
     * @param userDto
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody UserDto userDto, HttpSession session){
        return userService.login(userDto, session);
    }

    /**
     * 登出接口
     * @param session
     * @return
     */
    @ApiOperation("登出")
    @PostMapping("/logout")
    public ResponseResult loginOut(HttpSession session){
        session.removeAttribute("loginUser");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 展示个人信息接口
     * @param session
     * @return
     */
    @ApiOperation("展示个人信息")
    @GetMapping("show")
    public ResponseResult show(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        log.info(String.valueOf(user.getId()));
        return ResponseResult.okResult(userService.getById(user.getId()));
    }

}
